@extends('admin.common.main')
@section('cnt')
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                {!! authBtn('admin.apiuser.create','add','添加') !!}
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            {!! authBtn('admin.apiuser.edit','edit') !!}
            {!! authBtn('admin.apiuser.del','delete') !!}
        </script>
    </div>
</div>
@endsection
@section('js')
<script>
    layui.use(['form', 'table','http'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            http  = layui.http,
            apiurl =http.api;
        init();
        const  csrf = "{{csrf_token()}}";
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            init(data.field);
            return false;
        });
        function init(resulti){
            http.render(
                '{{route('admin.apiuser.index')}}',
                [[
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 60, title: 'ID', sort: true},
                    {field: 'username', width: 100, title: '账号'},
                    {field: 'password', width: 100, title: '密码'},
                    {field: 'created_at', width: 120, title: '创建时间', sort: true},
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]],
                resulti
            );
        }
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '用户添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{{route('admin.apiuser.create')}}',
                    success: function(layero, index){
                        console.log(layero, index);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            var id = obj.data.id;
            console.error(data,del);
            if (obj.event === 'edit') {
                var url = "{{route('admin.apiuser.edit',['id'=>1])}}";
                console.error("url",url);
                var url =  url.replace('1', id);
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: url,
                });
                return false;
            }
            else if (obj.event === 'delete') {
                var del = "{{route('admin.apiuser.destroy',['id'=>1])}}";
                layer.confirm('真的删除么', function (index) {
                    var url =  del.replace('1', id);
                    http.httpajax( url,{'_token':csrf},"DELETE",function(res) {
                        if(res.code === 200){
                            layer.msg(res.msg, {time: 1800, icon: 1});
                            obj.del();
                            init();
                        }else{
                            layer.msg(res.msg, {time: 1800, icon: 2});
                        }
                    });
                });
            }
        });

    });
</script>
@endsection
